package com.sbox;

import java.util.Date;

import com.sbox.db.BackupMgr;
import com.sbox.model.SBoxConfig;
import com.sbox.util.SBoxLog;
import com.sbox.util.Utils;

import android.app.Activity;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.CheckBox;
import android.widget.RadioButton;
import android.widget.TextView;

public class BackupActivity extends Activity {
	private TitleBar mTitleBar = null;
	private TextView mLastBackup = null;
	private Button mBtnBackupSD = null;
	private Button mBtnBackupEmail = null;
	private Button mBtnRestoreSD = null;
	
	private View.OnClickListener mBackListener = new View.OnClickListener() {		
		@Override
		public void onClick(View v) {			
			BackupActivity.this.finish();
		}
	};
	
	private View.OnClickListener mBtnBackupSDListener = new View.OnClickListener() {		
		@Override
		public void onClick(View v) {		
			SBoxLog.info("button backup.");
			BackupMgr bm = BackupMgr.getInstance(BackupActivity.this);
			bm.backupToSD();
			
			BackupActivity.this.finish();
		}
	};
	
	private View.OnClickListener mBtnBackupEmailListener = new View.OnClickListener() {		
		@Override
		public void onClick(View v) {			
			BackupMgr bm = BackupMgr.getInstance(BackupActivity.this);
			bm.backupToSD();
			
			BackupActivity.this.finish();
		}
	};
	
	private View.OnClickListener mBtnRestoreSDListener = new View.OnClickListener() {		
		@Override
		public void onClick(View v) {			
			BackupMgr bm = BackupMgr.getInstance(BackupActivity.this);
			bm.restoreFromSD();
			
			BackupActivity.this.finish();
		}
	};
	
	@Override
	public void onCreate(Bundle savedBundle)
	{
		super.onCreate(savedBundle);
		setContentView(R.layout.backup_view);
		
		mTitleBar = (TitleBar)findViewById(R.id.backup_title);
		mTitleBar.getButton1().setOnClickListener(mBackListener);
		mTitleBar.getButton2().setVisibility(View.INVISIBLE);
		
		mLastBackup = (TextView)findViewById(R.id.backup_last_time);
		
		mBtnBackupSD = (Button)findViewById(R.id.btn_backup_sd);
		mBtnBackupSD.setOnClickListener(mBtnBackupSDListener);
		
		mBtnBackupEmail = (Button)findViewById(R.id.btn_backup_email);
		mBtnBackupEmail.setOnClickListener(mBtnBackupEmailListener);
		
		mBtnRestoreSD = (Button)findViewById(R.id.btn_restore_sd);
		mBtnRestoreSD.setOnClickListener(mBtnRestoreSDListener);
		
		init();
	}
	
	private void init()
	{		
		SBoxConfig config = SBoxConfig.getInstance(this);
		Date time = null;
		
		if( config.mLastBackup == 0 ) {
			mLastBackup.setText(getText(R.string.str_backup_never));
		} else {
			time = new Date(config.mLastBackup);
			mLastBackup.setText( getText(R.string.str_backup_last_time) + time.toLocaleString() );
		}
	}
}
